package cn.knightzz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.constant.RoleType;
import cn.knightzz.entity.SysRole;
import cn.knightzz.entity.SysUser;
import cn.knightzz.entity.SysUserRole;
import cn.knightzz.mapper.SysRoleMapper;
import cn.knightzz.mapper.SysUserMapper;
import cn.knightzz.mapper.SysUserRoleMapper;
import cn.knightzz.service.SysRoleService;
import cn.knightzz.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javassist.tools.rmi.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author knightzz
 * @since 2021-04-09
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    SysRoleMapper roleMapper;

    @Resource
    SysUserRoleMapper userRoleMapper;

    @Resource
    SysUserService userService;

    private final Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Override
    public SysRole getRoleById(Integer id) {
        // 构造查询条件
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        // 根据id查询角色信息
        return roleMapper.selectOne(queryWrapper);
    }

    @Override
    public SysRole getRoleByRoleCode(String roleCode) {
        // 构造查询条件
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_code", roleCode);
        // 根据id查询角色信息
        return roleMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<SysRole> selectRolePage(Long pageSize, Long pageNum, String query) {
        // 构造查询对象
        Page<SysRole> page = new Page<>();
        // 设置当前页码
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return roleMapper.selectRolePage(page, query);
    }

    /**
     * 添加角色信息
     *
     * @param role
     * @return
     */
    @Override
    public Boolean addRole(SysRole role) throws ObjectNotFoundException {

        if (ObjectUtil.isNull(role)) {
            throw new ObjectNotFoundException("角色对象为空!");
        }
         // 角色不存在
        int row = roleMapper.insert(role);
        return row == 1;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteRole(Long id) {

        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", id);
        // 判断该角色是否在使用
        SysUserRole userRole = userRoleMapper.selectOne(queryWrapper);
        // 如果该角色和用户已经关联了 , 那么 这个角色无删除
        if(ObjectUtil.isNotNull(userRole)){
            return false;
        }
        int row = roleMapper.deleteById(id);
        return row != 0;
    }

    /**
     * 更新角色信息
     *
     * @param role
     * @return
     */
    @Override
    public Boolean updateRole(SysRole role) {
        if (ObjectUtil.isNull(role)) {
            return false;
        }
        try {
            roleMapper.updateById(role);
        }catch (Exception e) {
            log.error("updateRole 更新失败!");
            return false;
        }
        return true;
    }

    /**
     * 根据角色名查询角色信息
     *
     * @param roleName
     * @return
     */
    @Override
    public SysRole getRoleByRoleName(String roleName) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", roleName);
        return roleMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysRole> getRoleList(Long userId) {

        // 获取当前用户的类型
        SysUser userById = userService.getUserById(userId);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
       if(userById.getType() != RoleType.SUPER_ADMIN){
           // 如果不是超级管理员的话
           queryWrapper.ne("id", RoleType.SUPER_ADMIN);
       }
        return roleMapper.selectList(queryWrapper);
    }
}
